package com.library.admin.listener;

import books.storage.service.BookDto;
import books.storage.service.EntityNotFoundException_Exception;

import com.library.admin.AdmLibrary;
import com.library.admin.list.BookTable;
import com.library.admin.service.LibraryService;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class RemoveBookListener implements ClickListener {
	private static final long serialVersionUID = -6420428662526202605L;
	
	private BookTable bookTable;
	private String bookIsbn;

	public RemoveBookListener(BookTable bookTable, String bookIsbn) {
		this.bookTable = bookTable;
		this.bookIsbn = bookIsbn;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) bookTable.getApplication();
		try {
			BookDto bookToRemove = LibraryService.getServiceBook().findBook(bookIsbn);
			LibraryService.getServiceBook().removeBook(bookToRemove);
			libraryApplication.getMainWindow().showNotification("Le livre a été supprimé.", Notification.TYPE_HUMANIZED_MESSAGE);
		}
		catch (EntityNotFoundException_Exception e) {
			libraryApplication.getMainWindow().showNotification("Impossible de supprimer ce livre.", Notification.TYPE_ERROR_MESSAGE);
			e.printStackTrace();
		}
		bookTable.removeItem(bookIsbn);
	}
}
